package com.moleq.dialog;

import java.sql.Date;
import java.util.Calendar;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.TimePicker.OnTimeChangedListener;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.logic.TransactionManager;
import com.moleq.mgpos.activities.SaleActivity;
import com.moleq.mgpos.activities.R;
import com.moleq.service.ServiceManager;
import com.moleq.util.MoleQUtil;

public class TimeSettingSelectionDialog extends MoleQBaseDialog {

	private Button btnConfirmDate;
	private Button btnClose;
	private TextView tv_datetime;
	private TimePicker tp;
	private String age_time;
	private int viewid;
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			
			switch (arg0.getId()) {
			case R.id.btn_time_Confirm:
				onCloseWithReturn(viewid,age_time);
				break;
			case R.id.btn_time_Close:
				dismiss();
				break;
			}	
		}
	};
	


	public TimeSettingSelectionDialog(Context context) {
		super(context, R.style.dialog, context);
		constructor(context);
	}

	private void constructor(Context context) {
		mContext = context;
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.dialog_setting_time_selection);
		this.setCancelable(false);

		btnConfirmDate = (Button) findViewById(R.id.btn_time_Confirm);
		btnConfirmDate.setOnClickListener(mOnClickListener);

		btnClose = (Button) findViewById(R.id.btn_time_Close);
		btnClose.setOnClickListener(mOnClickListener);

		tv_datetime = (TextView) findViewById(R.id.tv_datetime);
		tp = (TimePicker) findViewById(R.id.tp_time);
	}

	public void show(int vid,String timeText) {
		super.show();
		viewid = vid;		
		
		Calendar calendar=Calendar.getInstance();
		int mHour=calendar.get(Calendar.HOUR_OF_DAY);
		int mMinute=calendar.get(Calendar.MINUTE);
		if (!timeText.equals("")) {
			mHour = Integer.valueOf(timeText.substring(0, 2));
			mMinute = Integer.valueOf(timeText.substring(3, 5));
		}

		tv_datetime.setText(String.format("%02d", mHour)+":"+String.format("%02d", mMinute));
		if (mHour > 24) {
			mHour = mHour%24;
		}
		age_time = mHour+":"+mMinute;
		tp.setIs24HourView(true);
		tp.setCurrentHour(mHour);  
        tp.setCurrentMinute(mMinute); 
        tp.setOnTimeChangedListener(otcl);
	}
	
	 OnTimeChangedListener otcl=new OnTimeChangedListener(){  
         public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 
         	age_time = String.format("%02d", hourOfDay)+":"+String.format("%02d", minute);
//             tv.setText("你选择了时："+hourOfDay+"\n");  
//             tv.append("你选择了分："+minute+"\n");  
         }  
     };

}
